package com.et.util;

import android.text.TextUtils;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;


public final class FtpUtil {

    private static final Executor netThread = Executors.newSingleThreadExecutor();

    public static final class Request {
        public String host;
        public int port;
        public String userName;
        public String password;
        public String remoteFolderPath;
        public String fileName;
        public FileInputStream inputStream;
    }

    public static final class Response {

        public boolean success;
        public String message;

        public Response(boolean success, String message) {
            this.success = success;
            this.message = message;
        }

        public static Response newInstance(Request param) {
            if (param == null) return new Response(false, "请求参数为空");
            if (TextUtils.isEmpty(param.host)) return new Response(false, "服务器地址为空");
            if (TextUtils.isEmpty(param.userName)) return new Response(false, "用户信息异常");
            if (TextUtils.isEmpty(param.password)) return new Response(false, "用户信息异常");
            if (TextUtils.isEmpty(param.remoteFolderPath)) return new Response(false, "远程目录空");
            if (TextUtils.isEmpty(param.fileName)) return new Response(false, "文件名空");
            if (param.inputStream == null) return new Response(false, "文件IO流异常");
            return new Response(true, "请求中");
        }
    }


    public static FtpUtil getInstance() {
        return Holder.ins;
    }

    private static class Holder {
        private static final FtpUtil ins = new FtpUtil();
    }

    public Single<Response> uploadFile(Request request) {
        return Single.just(request)
                .map(param -> {
                    Response response = Response.newInstance(param);
                    if (response.success) {
                        FTPClient ftpClient = new FTPClient();
                        ftpClient.connect(request.host, request.port);
                        ftpClient.login(request.userName, request.password);
                        int reply = ftpClient.getReplyCode();
                        if (FTPReply.isPositiveCompletion(reply)) {
                            ftpClient.makeDirectory(request.remoteFolderPath);
                            ftpClient.changeWorkingDirectory(request.remoteFolderPath);
                            ftpClient.setBufferSize(1024);
                            ftpClient.setControlEncoding("UTF-8");
                            // ftpClient.enterLocalPassiveMode(); // Linux服务器
                            ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 设置文件类型及采用被动传输方式
                            try (FileInputStream inputStream = request.inputStream) {
                                ftpClient.storeFile(request.fileName, inputStream);
                                response.success = true;
                                response.message = "传输完成";
                            } catch (IOException e) {
                                response.success = false;
                                response.message = "文件IO异常";
                            }
                            ftpClient.logout();
                            ftpClient.disconnect();
                        } else {
                            ftpClient.disconnect();
                            response.success = false;
                            response.message = "登陆异常";
                        }
                    }
                    return response;
                })
                .subscribeOn(Schedulers.from(netThread))
                .observeOn(Schedulers.from(netThread));
    }

}
